﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.Linq;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Iridescent.Models;

namespace Iridescent.ViewModels
{
    public class NewOrEditCourseViewModel
    {
        public string ApplicationTitle { get; private set; }
        public string PageTitle { get; private set; }
        public Course Course { get; private set; }

        public IEnumerable<string> CourseNames
        {
            get
            {
                return (from c in App.LocalDatabase.Courses
                        select c.Name).Distinct();
            }
        }

        public IEnumerable<string> Loactions
        {
            get
            {
                return (from c in App.LocalDatabase.Courses
                        select c.Location).Distinct();
            }
        }

        public static NewOrEditCourseViewModel CreateNewCourseViewMode(string day)
        {
            var dayOfWeek = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), day, true);

            return new NewOrEditCourseViewModel
            {
                ApplicationTitle = DateTimeFormatInfo.CurrentInfo.GetDayName(dayOfWeek),
                PageTitle = "新建课程",
                Course = new Course
                {
                    Day = dayOfWeek,
                    StartTime = new DateTime(1753, 1, 1, 0, 0, 0, 0),
                    EndTime = new DateTime(1753, 1, 1, 0, 0, 0)
                }
            };
        }

        public static NewOrEditCourseViewModel CreateEditCOurseViewModel(string id)
        {
            var course = App.LocalDatabase.Courses.First(c => c.Id == Int32.Parse(id));

            return new NewOrEditCourseViewModel
            {
                ApplicationTitle = DateTimeFormatInfo.CurrentInfo.GetDayName(course.Day),
                PageTitle = "编辑课程",
                Course = course,
            };
        }

        public void Submit()
        {
            if (PageTitle == "新建课程")
            {
                App.TimetableViewModel
                    .CourseListViewModels[App.TimetableViewModel.SelectedCourseListIndex]
                    .Courses.Add(Course);

                App.LocalDatabase.Courses.InsertOnSubmit(Course);
            }
            App.LocalDatabase.SubmitChanges();
        }

        public void Discard()
        {
            if (PageTitle == "编辑课程")
            {
                App.LocalDatabase.Refresh(RefreshMode.OverwriteCurrentValues, Course);
            }
        }

    }
}
